package com.dwxt.community.aiequipment.util;

import com.dwxt.community.aiequipment.aibin.entity.*;
import com.dwxt.community.aiequipment.aibin.service.*;
import com.dwxt.community.aiequipment.enumeration.*;
import org.apache.commons.lang.*;
import org.slf4j.*;

import java.util.*;

/**
 * @author
 * @program: intelligentcity
 * @description: 返回给分类桶数据
 * @create: 2020/04/15
 */
public class ReplyFenleiUtil {

    private static Logger logger = LoggerFactory.getLogger(ReplyFenleiUtil.class);

    /**
     * 构建握手返回数据
     *
     * @return
     */
    public static int[] replyWsOrRecord(TrashCanService trashCanService, String deviceId, Integer statusCode) {
        if (StringUtils.isNotBlank(deviceId)) {

            //获取所有的内桶
            List<TrashInner> trashInners = trashCanService.listAllTrashInner(deviceId);
            int[] resInts;
            resInts = ReplyBaseUtil.ok(WsConstant.WC_REPLY_TOTAL_LENGTH + trashInners.size() * 4);
            resInts[10] = 0;//通讯状态码
            resInts[11] = statusCode;//业务状态码
            //机构码
            String orgCode = "012" + "0";
            char[] orgCodes = orgCode.toCharArray();
            for (int i = 0; i < orgCodes.length; i++) {
                resInts[i + 12] = orgCodes[i];//业主编码
            }
            //单位编码
            String unitCode = "0123";
            char[] unitCodes = unitCode.toCharArray();
            for (int i = 0; i < unitCodes.length; i++) {
                resInts[i + 16] = unitCodes[i];
            }
            unitCodes = null;
            //获取设备配置
            TrashSet trashSet = trashCanService.selectOrgSetPd(deviceId);
            if (trashSet == null) {
                trashSet = trashCanService.selectOrgSetPd("moren");
            }
            String ggpopen = trashSet.getGuangGaoScreenOpen();
            int index = ggpopen.indexOf(':');
            resInts[20] = Integer.parseInt(ggpopen.substring(index + 1) == "00" ? "0" : ggpopen.substring(index + 1));
            resInts[21] = Integer.parseInt(ggpopen.substring(0, index));

            String ggpclose = trashSet.getGuangGaoScreenClose();
            index = ggpclose.indexOf(':');
            resInts[22] = Integer.parseInt(ggpclose.substring(index + 1) == "00" ? "0" : ggpclose.substring(index + 1));
            resInts[23] = Integer.parseInt(ggpclose.substring(0, index));

            String ggdopen = trashSet.getGuangGaoLedOpen();
            index = ggdopen.indexOf(':');
            resInts[24] = Integer.parseInt(ggdopen.substring(index + 1) == "00" ? "0" : ggdopen.substring(index + 1));
            resInts[25] = Integer.parseInt(ggdopen.substring(0, index));

            String ggdclose = trashSet.getGuangGaoLedClose();
            index = ggdclose.indexOf(':');
            resInts[26] = Integer.parseInt(ggdclose.substring(index + 1) == "00" ? "0" : ggdclose.substring(index + 1));
            resInts[27] = Integer.parseInt(ggdclose.substring(0, index));
            String qropen = trashSet.getQrCodeLightingOpen();
            index = qropen.indexOf(':');
            resInts[28] = Integer.parseInt(qropen.substring(index + 1) == "00" ? "0" : qropen.substring(index + 1));
            resInts[29] = Integer.parseInt(qropen.substring(0, index));
            String qrclose = trashSet.getQrCodeLightingClose();
            index = qrclose.indexOf(':');
            resInts[30] = Integer.parseInt(qrclose.substring(index + 1) == "00" ? "0" : qrclose.substring(index + 1));
            resInts[31] = Integer.parseInt(qrclose.substring(0, index));
            String tsyopen = trashSet.getWarningToneOpen();
            index = tsyopen.indexOf(':');
            resInts[32] = Integer.parseInt(tsyopen.substring(index + 1) == "00" ? "0" : tsyopen.substring(index + 1));
            resInts[33] = Integer.parseInt(tsyopen.substring(0, index));
            String tsyclose = trashSet.getWarningToneClose();
            index = tsyclose.indexOf(':');
            resInts[34] = Integer.parseInt(tsyclose.substring(index + 1) == "00" ? "0" : tsyclose.substring(index + 1));
            resInts[35] = Integer.parseInt(tsyclose.substring(0, index));
            String wifiopen = trashSet.getWifiOpen();
            index = wifiopen.indexOf(':');
            resInts[36] = Integer.parseInt(wifiopen.substring(index + 1) == "00" ? "0" : wifiopen.substring(index + 1));
            resInts[37] = Integer.parseInt(wifiopen.substring(0, index));
            String wificlose = trashSet.getWifiClose();
            index = wificlose.indexOf(':');
            resInts[38] = Integer.parseInt(wificlose.substring(index + 1) == "00" ? "0" : wificlose.substring(index + 1));
            resInts[39] = Integer.parseInt(wificlose.substring(0, index));
            resInts[40] = Integer.parseInt(trashSet.getDisinfectCycle());
            resInts[41] = Integer.parseInt(trashSet.getDisinfectDuration());
            resInts[42] = Integer.parseInt(trashSet.getShakeHandsCycle());
            int begin = 43;
            // todo 待机构码确认后再做处理 "c82fea22e4034860869b374cceeebeeb".equals("orgId")
            if (false) {
                for (int i = 0; i < trashInners.size(); i++) {
                    resInts[begin + i * 4] = trashInners.get(i).getTdmId();
                    resInts[begin + 1 + i * 4] = 80;
                    resInts[begin + 2 + i * 4] = 100;
                    resInts[begin + 3 + i * 4] = 0;
                }
            } else {
                for (int i = 0; i < trashInners.size(); i++) {
                    resInts[begin + i * 4] = trashInners.get(i).getTdmId();
                    resInts[begin + 1 + i * 4] = 95;
                    resInts[begin + 2 + i * 4] = 100;
                    resInts[begin + 3 + i * 4] = 0;
                }
            }
            // 计算响应CRC
            return DataHandler.buildCrcArr(resInts);
        } else {
            logger.error("这个设备编号不存在");
            //回复默认字节数组
            return ReplyBaseUtil.bulidCrcBaseResponseInts(WsConstant.WC_REPLY_TOTAL_LENGTH, 0, MainBoardConstant.DEVICE_EXIST_NO);
        }

    }

    /**
     * 构建验证返回数据
     */
    public static int[] replyVerify(int[] cordNo) {
        int totalLen = WsConstant.VERIFY_REPLY_TOTAL_LENGTH + cordNo.length + 1;
        int[] ints = ReplyBaseUtil.ok(totalLen);
        for (int i = 0; i < cordNo.length; i++) {
            ints[12 + i] = cordNo[i];
        }
        //设置卡号结束值
        ints[totalLen - 3] = 0;
        return DataHandler.buildCrcArr(ints);
    }
}


